home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / ddj9304.zip / WAVELET.ZIP / DSP_CTRL.C < prev    next >
Text File  |  1992-05-17  |  14KB  |  509 lines

  1. /* DSP_CTRL.C */
  2.  
  3. #define DSP_CTRL
  4.  
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <process.h>
  8. #include <dos.h>
  9.  
  10. #include "dsp_ctrl.h"
  11.  
  12. #define ZPB32                                0x00
  13. #define ZPB34                                0x01
  14. #define CODYBOARD                        0x02
  15. #define DECOMP_ADDR_DSP32        0x500
  16. #define WAVELET_ADDR_DSP32    0x0608
  17. #define DECOMP_ADDR_DSP34        0x0350
  18. #define WAVELET_ADDR_DSP34    0x0478
  19.  
  20. int _pcr;
  21. unsigned _board_addr, _pcr_addr, _pcrh_addr, _par_addr, _pare_addr;
  22. unsigned _pir_addr, _rbisr_addr, _sioc_addr, _id_reg_addr;
  23.  
  24. unsigned _wavelet_filt_addr, _decomp_coeff_addr;
  25. unsigned *_trace_scale_addr;
  26. unsigned *_decomp_data2, *_decomp_data4, *_decomp_data6;
  27. unsigned _decomp_datalen;
  28. unsigned _decomp_data2_DSP32[2] = {0x01e8, 0xfffc};
  29. unsigned _decomp_data4_DSP32[2] = {0x01e0, 0xfff4};
  30. unsigned _decomp_data6_DSP32[2] = {0x01d8, 0xffec};
  31. unsigned _decomp_data2_DSP34[4] = {0x01e0, 0x0000, 0xfffc, 0xffff};
  32. unsigned _decomp_data4_DSP34[4] = {0x01d8, 0x0000, 0xfff4, 0xffff};
  33. unsigned _decomp_data6_DSP34[4] = {0x01d0, 0x0000, 0xffec, 0xffff};
  34. unsigned _trace_scale_addrDSP32[8] = {0x0524, 0x0540, 0x055c, 0x0578,
  35.                                                                             0x0594, 0x05b0, 0x05cc, 0x05e8};
  36. unsigned _trace_scale_addrDSP34[8] = {0x0390, 0x03ac, 0x03c8, 0x03e4,
  37.                                                                             0x0400, 0x041c, 0x0438, 0x0454};
  38. float _scale_data[10] = {0.001155796991, 0.002311593982, 0.005778984955,
  39.                                                  0.01155796991, 0.02311593982, 0.05778984955,
  40.                                                  0.1155796991, 0.2311593982, 0.5778984955,
  41.                                                  1.155796991};
  42.  
  43. char  codefileDSP32[] = "anlyzr32.dsp";
  44. char  codefileDSP32C[] = "anlyz32c.dsp";
  45.  
  46. void RunZPB32(void);
  47. void RunCODY(void);
  48. void RunZPB34(void);
  49. void HaltZPB32(void);
  50. void HaltCODY(void);
  51. void HaltZPB34(void);
  52. void SetDMAaddrZPB32(unsigned long addr);
  53. void SetDMAaddrZPB34(unsigned long addr);
  54. void DownloadIntZPB34(unsigned addr, unsigned *data, unsigned count);
  55. void DownloadIntZPB32(unsigned addr, unsigned *data, unsigned count);
  56. unsigned long ConvIEEEtoDSP32(float a);
  57. unsigned int ACKpir16bitZPB32 (void);
  58. unsigned int ACKpir16bitZPB34(void);
  59. unsigned WaitZPB32(unsigned count);
  60. unsigned WaitZPB34(unsigned count);
  61. void DownloadDSPexec(char *filename);
  62. void (*DownloadInt)();
  63.  
  64. void DSPboardConfigure(void)
  65. {
  66.     FILE *cfg;
  67.     size_t count;
  68.     char DSPboardtype, answer;
  69.  
  70.     /* attempt to open the DSP board configuration file */
  71.     if (!(cfg = fopen("analyzer.cfg", "rb")))
  72.     {
  73.         /* create a new configuration file and fill it */
  74.         puts("\nConfiguration file ANALYZER.CFG not found");
  75.         cfg = fopen("analyzer.cfg", "wb");
  76.         do
  77.         {
  78.             puts("\nEnter type of DSP board to be used");
  79.             printf("0 - ZPB32, 1 - ZPB34, 2 - CODYBOARD: ");
  80.             DSPboardtype = (char) getche();
  81.         } while ((DSPboardtype != '0') && (DSPboardtype != '1') &&
  82.                             (DSPboardtype != '2'));
  83.  
  84.         DSPboardtype -= 0x30;
  85.         do
  86.         {
  87.             puts("\nEnter I/O bus address of the DSP board");
  88.             printf("in hexidecimal (e.g. 0x300 entered as 300) : ");
  89.             scanf("%x", &_board_addr);
  90.             printf("\nIs the I/O bus addr %xH? (Y/N): ", _board_addr);
  91.             answer = getche();
  92.             answer &= 0xdf;
  93.         } while (answer != 'Y');
  94.  
  95.         putc((int) DSPboardtype, cfg); /* write configuration data to file */
  96.         putw((int) _board_addr, cfg);
  97.     }
  98.     else
  99.     {
  100.         /* read the DSP board type */
  101.         count = fread((void *) &DSPboardtype, sizeof(char), 1, cfg);
  102.         if (!count)
  103.         {
  104.             puts("\nUnexpected End-of-file encountered while");
  105.             puts("reading configuration file ANALYZER.CFG");
  106.             fclose(cfg);
  107.             exit(1);
  108.         }
  109.  
  110.         /* read the location of the DSP board in the I/O map */
  111.         count = fread((void *) &_board_addr, sizeof(unsigned), 1, cfg);
  112.         if (!count)
  113.         {
  114.             puts("\nUnexpected End-of-file encountered while");
  115.             puts("reading configuration file ANALYZER.CFG");
  116.             fclose(cfg);
  117.             exit(2);
  118.         }
  119.     }
  120.  
  121.     fclose(cfg); /* done with the configuration file */
  122.  
  123.     /* set up DSP board register addresses, memory addresses,
  124.          data arrays, and I/O routines according the the DSP board type */
  125.     switch (DSPboardtype)
  126.     {
  127.         case ZPB32:
  128.             _par_addr = _board_addr;
  129.             _pdr_addr = _board_addr + 0x02;
  130.             _pcr_addr = _board_addr + 0x07;
  131.             _pir_addr = _board_addr + 0x08;
  132.             _pcrh_addr = 0;                   /* non-existent on DSP32 processor */
  133.             _pare_addr = 0;                   /* non-existent on DSP32 processor */
  134.  
  135.             _sioc_addr = _board_addr + 0x0f;  /* miscellaneous registers */
  136.             _id_reg_addr = _board_addr + 0x0f;
  137.             _rbisr_addr = 0;                  /* non-existent on ZPB32 board */
  138.             _bitmap0 = 0x0678L;
  139.             _bitmap1 = 0x1fdcL;
  140.  
  141.             _wavelet_filt_addr = WAVELET_ADDR_DSP32;
  142.             _trace_scale_addr = _trace_scale_addrDSP32;
  143.             _decomp_coeff_addr = DECOMP_ADDR_DSP32;
  144.             _decomp_data2 = _decomp_data2_DSP32;
  145.             _decomp_data4 = _decomp_data4_DSP32;
  146.             _decomp_data6 = _decomp_data6_DSP32;
  147.             _decomp_datalen = 2;
  148.  
  149.             SetDMAaddr = SetDMAaddrZPB32;
  150.             DownloadInt = DownloadIntZPB32;
  151.             RunDSP = RunZPB32;
  152.             HaltDSP = HaltZPB32;
  153.             ACKpir16bit = ACKpir16bitZPB32;
  154.             WaitDSP = WaitZPB32;
  155.             DownloadDSPexec(codefileDSP32);
  156.             break;
  157.  
  158.         case ZPB34:
  159.             _par_addr = _board_addr;          /* bank 0 */
  160.             _pdr_addr = _board_addr + 0x04;
  161.  
  162.             _pcr_addr = _board_addr + 0x06;   /* bank 1 */
  163.  
  164.             _pir_addr = _board_addr;          /* bank 2 */
  165.             _pcrh_addr = _board_addr + 0x04;
  166.             _pare_addr = _board_addr + 0x06;
  167.  
  168.             _rbisr_addr = _board_addr + 0x0b; /* miscellaneous registers */
  169.             _sioc_addr = _board_addr + 0x0e;
  170.             _id_reg_addr = _board_addr + 0x0f;
  171.             _bitmap0 = 0x0528L;
  172.             _bitmap1 = 0x1e8cL;
  173.  
  174.             _wavelet_filt_addr = WAVELET_ADDR_DSP34;
  175.             _trace_scale_addr = _trace_scale_addrDSP34;
  176.             _decomp_coeff_addr = DECOMP_ADDR_DSP34;
  177.             _decomp_data2 = _decomp_data2_DSP34;
  178.             _decomp_data4 = _decomp_data4_DSP34;
  179.             _decomp_data6 = _decomp_data6_DSP34;
  180.             _decomp_datalen = 4;
  181.  
  182.             SetDMAaddr = SetDMAaddrZPB34;
  183.             DownloadInt = DownloadIntZPB34;
  184.             RunDSP = RunZPB34;
  185.             HaltDSP = HaltZPB34;
  186.             ACKpir16bit = ACKpir16bitZPB34;
  187.             WaitDSP = WaitZPB34;
  188.             DownloadDSPexec(codefileDSP32C);
  189.             break;
  190.  
  191.         case CODYBOARD:
  192.             _par_addr = _board_addr;
  193.             _pdr_addr = _board_addr + 0x02;
  194.             _pcr_addr = _board_addr + 0x07;
  195.             _pir_addr = _board_addr + 0x08;
  196.             _pcrh_addr = 0;                   /* non-existent on DSP32 processor */
  197.             _pare_addr = 0;                   /* non-existent on DSP32 processor */
  198.  
  199.             _sioc_addr = 0;                   /* non-existent on CODYBOARD */
  200.             _id_reg_addr = _board_addr + 0x0f;
  201.             _rbisr_addr = 0;                  /* non-existent on ZPB32 board */
  202.             _bitmap0 = 0x0678L;
  203.             _bitmap1 = 0x1fdcL;
  204.  
  205.             _wavelet_filt_addr = WAVELET_ADDR_DSP32;
  206.             _trace_scale_addr = _trace_scale_addrDSP32;
  207.             _decomp_coeff_addr = DECOMP_ADDR_DSP32;
  208.             _decomp_data2 = _decomp_data2_DSP32;
  209.             _decomp_data4 = _decomp_data4_DSP32;
  210.             _decomp_data6 = _decomp_data6_DSP32;
  211.             _decomp_datalen = 2;
  212.  
  213.             SetDMAaddr = SetDMAaddrZPB32;
  214.             DownloadInt = DownloadIntZPB32;
  215.             RunDSP = RunCODY;
  216.             HaltDSP = HaltCODY;
  217.             ACKpir16bit = ACKpir16bitZPB32;
  218.             WaitDSP = WaitZPB32;
  219.             DownloadDSPexec(codefileDSP32);
  220.             break;
  221.  
  222.         default:
  223.             puts("\nUndefined DSP board type in configuration file ANALYZER.CFG");
  224.             exit(3);
  225.         }
  226. }
  227.  
  228.  
  229. void RunZPB32(void)
  230. {
  231.     outportb(_sioc_addr, 0x05); /* set up serial I/O control for codec */
  232.     outportb(_pcr_addr, 0x00); /* halt DSP32 */
  233.     outportb(_pcr_addr, 0x01); /* reset DSP32 */
  234.     outportb(_pcr_addr, 0x1f); /* run DSP32 with autoincrement DMA and ... */
  235. }                            /* 16-bit PIR interrupt vector enabled */
  236.  
  237.  
  238. void RunCODY(void)
  239. {
  240.     outportb(_pcr_addr, 0x00); /* halt DSP32 */
  241.     __emit__(0x90);
  242.     outportb(_pcr_addr, 0x01); /* reset DSP32 */
  243.     __emit__(0x90);
  244.     outportb(_pcr_addr, 0x1f); /* run DSP32 with autoincrement DMA and ... */
  245. }                            /* 16-bit PIR interrupt vector enabled */
  246.  
  247.  
  248. void RunZPB34(void)
  249. {
  250.     outportb(_sioc_addr, 0x05); /* set up serial I/O control for codec */
  251.     outportb(_rbisr_addr, 1);
  252.     outport(_pcr_addr, 0x00); /* halt DSP32C */
  253.     outport(_pcr_addr, 0x01); /* reset DSP32C */
  254.     outport(_pcr_addr, 0x07); /* set PCR register address map to PCR[1] = 1, */
  255.     outportb(_rbisr_addr, 2);
  256.     outport(_pcrh_addr, 0x02); /* and PCR[9] = 1 */
  257.     outportb(_rbisr_addr, 1);
  258.     outport(_pcr_addr, 0x021f); /* run DSP32C with autoincrement DMA and ... */
  259. }                            /* 16-bit PIR interrupt vector enabled */
  260.  
  261.  
  262. void HaltZPB32(void)
  263. {
  264.     outportb(_pcr_addr, 0x18); /* halt DSP and set up for post-increment DMA */
  265.     outportb(_sioc_addr, 0x33); /* deactivate the serial I/O to the codec */
  266. }
  267.  
  268.  
  269. void HaltCODY(void)
  270. {
  271.     outportb(_pcr_addr, 0x18); /* halt DSP and set up for post-increment DMA */
  272. }
  273.  
  274.  
  275. void HaltZPB34(void)
  276. {
  277.     outportb(_rbisr_addr, 1);
  278.     outport(_pcr_addr, 0x1e); /* halt DSP and set up for post-increment DMA */
  279.     outportb(_rbisr_addr, 2);
  280.     outport(_pcrh_addr, 0x02);
  281.     outportb(_rbisr_addr, 0);
  282.     outportb(_sioc_addr, 0x33);  /* deactivate the serial I/O to the codec */
  283. }
  284.  
  285.  
  286. void SetDMAaddrZPB32(unsigned long addr)
  287. {
  288.     outport(_par_addr, (unsigned) addr);
  289. }
  290.  
  291.  
  292. void SetDMAaddrZPB34(unsigned long addr)
  293. {
  294.     unsigned *addr_parts = (unsigned *) &addr;
  295.  
  296.     outportb(_rbisr_addr, 2);
  297.     outport(_pare_addr, addr_parts[1]); /* set high word of address */
  298.     outportb(_rbisr_addr, 0);
  299.     outport(_par_addr, addr_parts[0]); /* set low word of address */
  300. }
  301.  
  302.  
  303. void DownloadIntZPB32(unsigned addr, unsigned *data, unsigned count)
  304. {
  305.     unsigned int i;
  306.  
  307.     _pcr = inportb(_pcr_addr); /* save current PCR value */
  308.     __emit__(0x90);
  309.     outportb(_pcr_addr, (unsigned char) _pcr | 0x18); /* post-increment DMA */
  310.     __emit__(0x90);
  311.     outport(_par_addr, addr);
  312.  
  313.     for (i = 0; i < count; i++)   /* download 16-bit integer */
  314.         outport(_pdr_addr, data[i]);
  315.  
  316.     __emit__(0x90);
  317.     outportb(_pcr_addr, (unsigned char) _pcr); /* restore original PCR value */
  318. }
  319.  
  320.  
  321. void DownloadIntZPB34(unsigned addr, unsigned *data, unsigned count)
  322. {
  323.     unsigned int i;
  324.  
  325.     outportb(_rbisr_addr, 1);
  326.     _pcr = inport(_pcr_addr); /* save current PCR value */
  327.     outport(_pcr_addr, (_pcr | 0x0018) & 0xfeff); /* post-increment DMA */
  328.     outportb(_rbisr_addr, 2);
  329.     outport(_pare_addr, 0x0000);
  330.     outportb(_rbisr_addr, 0);
  331.     outport(_par_addr, addr);
  332.  
  333.     for (i = 0; i < count; i++)   /* download 16-bit integer */
  334.         outport(_pdr_addr, data[i]);
  335.  
  336.     outportb(_rbisr_addr, 1);
  337.     outport(_pcr_addr, _pcr); /* restore original PCR value */
  338. }
  339.  
  340.  
  341. /* Convert an IEEE 754 format number to DSP32 floating point format */
  342. unsigned long ConvIEEEtoDSP32(float a)
  343. {
  344.     unsigned long sn, ex, fr;
  345.     union {
  346.                     float b;
  347.                     unsigned long i;
  348.                 } x;
  349.  
  350.     x.b = a;
  351.     sn = x.i & 0x80000000L;        /* shift and mask off sign bit */
  352.     ex = (x.i >> 23) & 0xffL;      /* shift and mask off bits of exponent */
  353.     fr = (x.i << 8) & 0x7fffffffL; /* shift and mask off bits of mantissa */
  354.     if ((!sn && ex) || (sn && fr)) /* twiddle exponent */
  355.         ex += 1;
  356.  
  357.     if (sn)
  358.         fr = -fr;  /* two's complement of the mantissa */
  359.  
  360.     return(sn | fr | ex);
  361. }
  362.  
  363.  
  364. void DownloadFiltCoeffs(float *filters)
  365. {
  366.     int i;
  367.     unsigned long dsp32float[12];
  368.     unsigned *dsp_data = (unsigned *) dsp32float;
  369.  
  370.     for (i = 0; i < 12; i++) /* download each float as two integers */
  371.         dsp32float[i] = ConvIEEEtoDSP32(filters[i]);
  372.  
  373.     DownloadInt(_wavelet_filt_addr, dsp_data, 24);
  374. }
  375.  
  376.  
  377. void DownloadDecompCoeffs(int filt_len)
  378. {
  379.     /* select coefficient set according to length of filters */
  380.     switch(filt_len)
  381.     {
  382.         case FILTER_LENGTH_2:
  383.             DownloadInt(_decomp_coeff_addr, _decomp_data2, _decomp_datalen);
  384.             break;
  385.         case FILTER_LENGTH_4:
  386.             DownloadInt(_decomp_coeff_addr, _decomp_data4, _decomp_datalen);
  387.             break;
  388.         case FILTER_LENGTH_6:
  389.             DownloadInt(_decomp_coeff_addr, _decomp_data6, _decomp_datalen);
  390.     }
  391. }
  392.  
  393.  
  394. void DownloadTraceScale(int trace_num, int scale_num)
  395. {
  396.     unsigned long dsp32float;
  397.     unsigned *dsp_data = (unsigned *) &dsp32float;
  398.  
  399.     dsp32float = ConvIEEEtoDSP32(_scale_data[scale_num]);
  400.     DownloadInt(_trace_scale_addr[trace_num], dsp_data, 2);
  401. }
  402.  
  403.  
  404. unsigned ACKpir16bitZPB32 (void)
  405. {
  406.     return(inport(_pir_addr));
  407. }
  408.  
  409.  
  410. unsigned ACKpir16bitZPB34(void)
  411. {
  412.     outportb(_rbisr_addr, 2);
  413.     return (inport(_pir_addr));
  414. }
  415.  
  416.  
  417. unsigned WaitZPB32(unsigned count)
  418. {
  419.     do
  420.     {
  421.         count-- ;
  422.     } while (!(inportb(_pcr_addr) & 0x40) && (count > 0));
  423.  
  424.     return (count); /* count reflects time left before timeout */
  425. }
  426.  
  427.  
  428. unsigned WaitZPB34(unsigned count)
  429. {
  430.     outportb(_rbisr_addr, 1);
  431.  
  432.     do
  433.     {
  434.         count--;
  435.     } while (!(inport(_pcr_addr) & 0x0040) && (count > 0));
  436.  
  437.     return (count); /* count reflects time left before timeout */
  438. }
  439.  
  440.  
  441. void DownloadDSPexec(char *file_name)
  442. {
  443.     FILE *fp;
  444.     int file_header[10];
  445.     int i, j;
  446.     unsigned raw_int[2];
  447.     unsigned long sect_header[10];
  448.     unsigned long sect_header_table_ptr;
  449.  
  450.     if ((fp = fopen(file_name, "rb")) == NULL)
  451.     {
  452.         fclose(fp);
  453.         printf("\nDSP COFF file %s not found\n", file_name);
  454.         exit(4);
  455.     }
  456.  
  457.     /* Read the file header. */
  458.  
  459.     if (fread(file_header, sizeof(file_header), 1, fp) == 0)
  460.     {
  461.         fclose(fp);
  462.         puts("\nUnexpected End-of-file encountered");
  463.         printf("while reading DSP COFF file %s\n", file_name);
  464.         exit(5);
  465.     }
  466.  
  467.     /* Seek past the optional header. */
  468.  
  469.     fseek(fp, (long) file_header[8], 1);
  470.     sect_header_table_ptr = ftell(fp);
  471.  
  472.     HaltDSP(); /* make sure DSP is halted and set up for DMA operation */
  473.  
  474.     for (i = 0; i < file_header[1]; i++)
  475.     {
  476.         fseek(fp, sect_header_table_ptr, 0);
  477.  
  478.         if (fread(sect_header, sizeof(sect_header), 1, fp) == 0)
  479.         {
  480.             fclose(fp);
  481.             puts("\nUnexpected End-of-file encountered");
  482.             printf("while reading DSP COFF file %s\n", file_name);
  483.             exit(6);
  484.         }
  485.  
  486.         sect_header_table_ptr = ftell(fp);
  487.         fseek(fp, sect_header[5], 0);
  488.  
  489.         SetDMAaddr(sect_header[2]);
  490.  
  491.         for (j = 0; j < sect_header[4]; j += 4)
  492.         {
  493.             if (fread(raw_int, sizeof(raw_int), 1, fp) == 0)
  494.             {
  495.                 fclose(fp);
  496.                 puts("\nUnexpected End-of-file encountered");
  497.                 printf("while reading DSP COFF file %s\n", file_name);
  498.                 exit(7);
  499.             }
  500.  
  501.             outport(_pdr_addr, raw_int[0]);
  502.             __emit__(0x90);
  503.             outport(_pdr_addr, raw_int[1]);
  504.         }
  505.     }
  506.  
  507.     fclose(fp);
  508. }
  509.